package com.kk.algorithm.binarysearchnorecursion;

/*
 * @Description:    二分查找（非递归）
 * @Author:         Jk_kang
 * @CreateDate:     2021/1/31 23:34
 * @Param:
 * @Return:
**/
public class BinarySearchNoRecursion {
    public static void main(String[] args) {
        //测试
        int[] arr = {1, 3, 8, 10, 11, 67, 100};
        int index = binarySearch (arr, 10);
        System.out.println ("index=" + index);
    }

    // 二分查找非递归
    public static int binarySearch(int[] arr, int target) {
        int left = 0;
        int right = arr.length - 1;
        while (left <= right) {// 说明可继续查找
            int mid = (left + right) / 2;
            if (arr[mid] == target) {
                return mid;
            } else if (arr[mid] > target) {
                // 需要向左查找
                right = mid - 1;
            } else {
                // 需要向右查找
                left = mid + 1;
            }
        }
        return -1;
    }
}
